/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package InterfaceCliente;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import pbl_cliente.*;

/**
 *
 * @author tayane
 */
public class TelaNovoCompromisso extends javax.swing.JFrame {

    /**
     * Creates new form novoCompromisso
     */
    private String DadosServidor;
    private PBL_cliente cli;
    private Socket cliente;
    private File arquivo = null;
    private List<String> listaUsuariosChamados = new ArrayList<String>();
    private List<String> list;
    DefaultListModel listaConvidados = new DefaultListModel(); //lista para armazenar os convidados
    

    public TelaNovoCompromisso() {

        initComponents();
        setResizable(false);
        setVisible(true);

        //this.ExibeListaConvidados.setModel(listaConvidados);
    }

    public TelaNovoCompromisso(Socket cliente, PBL_cliente cli, List<String> listUsersOnline) {

        initComponents();
        setVisible(true);
        this.cliente = cliente;
        this.cli = cli;    
        setLocationRelativeTo(null);
        
        addWindowListener(new WindowAdapter() {
 
            
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("Tela compromisso fechada.");
                TelaNovoCompromisso.this.cli.usuarioConsultando();
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        NomeAgendaEletronica = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        nomeCompromisso = new javax.swing.JTextField();
        DataCompromisso = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        localCompromisso = new javax.swing.JTextField();
        AnexarArquivo = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        DescricaoCompromisso = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        ExibeListaUsuarios = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        ExibeListaConvidados = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        adicionaConvidado = new javax.swing.JButton();
        cadastraCompromisso = new javax.swing.JButton();
        nomeArquivoAnexado = new javax.swing.JLabel();
        removeConvidadoSelecionado = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        horaTermino = new javax.swing.JFormattedTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        horaInicio = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();

        jPanel1.setBackground(java.awt.Color.white);
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        NomeAgendaEletronica.setFont(new java.awt.Font("Arial", 1, 18));
        NomeAgendaEletronica.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        NomeAgendaEletronica.setText("Agenda Eletrônica");
        jPanel1.add(NomeAgendaEletronica, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 10, -1, -1));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 39, 530, 10));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/InterfaceCliente/6098_32x32.png"))); // NOI18N
        jLabel1.setText("Novo Compromisso");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 50, -1, -1));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel2.setText("Título:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 114, -1, -1));

        jLabel4.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel4.setText("Local:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 176, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel5.setText("Descrição:");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 232, -1, -1));

        jLabel7.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel7.setText("Convidar usuários:");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, -1, -1));

        nomeCompromisso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeCompromissoActionPerformed(evt);
            }
        });
        jPanel1.add(nomeCompromisso, new org.netbeans.lib.awtextra.AbsoluteConstraints(58, 109, 460, -1));

        try {
            DataCompromisso.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        DataCompromisso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DataCompromissoActionPerformed(evt);
            }
        });
        jPanel1.add(DataCompromisso, new org.netbeans.lib.awtextra.AbsoluteConstraints(58, 139, 86, -1));

        jLabel8.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel8.setText("(dd/mm/aaaa)");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(148, 147, -1, -1));

        localCompromisso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                localCompromissoActionPerformed(evt);
            }
        });
        jPanel1.add(localCompromisso, new org.netbeans.lib.awtextra.AbsoluteConstraints(58, 171, 460, -1));

        AnexarArquivo.setFont(new java.awt.Font("Arial", 0, 12));
        AnexarArquivo.setText("Anexar Arquivo");
        AnexarArquivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnexarArquivoActionPerformed(evt);
            }
        });
        jPanel1.add(AnexarArquivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 287, -1, -1));

        DescricaoCompromisso.setColumns(20);
        DescricaoCompromisso.setRows(5);
        jScrollPane2.setViewportView(DescricaoCompromisso);

        jPanel1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 206, 430, 69));

        jScrollPane1.setViewportView(ExibeListaUsuarios);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 380, 180, 210));

        jScrollPane3.setViewportView(ExibeListaConvidados);

        jPanel1.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 380, 190, 212));

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel6.setText("Usuários");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 360, -1, -1));

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel9.setText("Lista de Convidados");
        jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 360, -1, -1));

        adicionaConvidado.setFont(new java.awt.Font("Arial", 0, 12));
        adicionaConvidado.setText("Adicionar =>");
        adicionaConvidado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        adicionaConvidado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionaConvidadoActionPerformed(evt);
            }
        });
        jPanel1.add(adicionaConvidado, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 470, -1, -1));

        cadastraCompromisso.setFont(new java.awt.Font("Arial", 1, 13));
        cadastraCompromisso.setText("Cadastrar Compromisso");
        cadastraCompromisso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastraCompromissoActionPerformed(evt);
            }
        });
        jPanel1.add(cadastraCompromisso, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 640, -1, -1));

        nomeArquivoAnexado.setFont(new java.awt.Font("Arial", 0, 12));
        nomeArquivoAnexado.setText("Nenhum arquivo anexado");
        jPanel1.add(nomeArquivoAnexado, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 290, -1, -1));

        removeConvidadoSelecionado.setFont(new java.awt.Font("Arial", 0, 12));
        removeConvidadoSelecionado.setText("Remover convidado selecionado");
        removeConvidadoSelecionado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        removeConvidadoSelecionado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeConvidadoSelecionadoActionPerformed(evt);
            }
        });
        jPanel1.add(removeConvidadoSelecionado, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 600, 210, -1));

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel10.setText("Arquivo:");
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 290, -1, -1));

        jLabel11.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel11.setText("Data:");
        jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 147, -1, -1));

        jLabel12.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel12.setText("Hora:");
        jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 150, -1, -1));

        try {
            horaTermino.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##h##min")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        horaTermino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                horaTerminoActionPerformed(evt);
            }
        });
        jPanel1.add(horaTermino, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 140, 80, -1));

        jLabel14.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel14.setText("às");
        jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 150, -1, -1));

        jLabel15.setFont(new java.awt.Font("Ubuntu Light", 0, 13));
        jLabel15.setText("de");
        jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 150, 0, 10));

        try {
            horaInicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##h##min")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jPanel1.add(horaInicio, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 140, 90, -1));

        jLabel3.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel3.setText("de");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 150, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DataCompromissoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DataCompromissoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DataCompromissoActionPerformed

    private void localCompromissoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localCompromissoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_localCompromissoActionPerformed

    private void adicionaConvidadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionaConvidadoActionPerformed
        String usuarioSelecionado = (String) ExibeListaUsuarios.getSelectedValue();
        System.out.println("entrei no botao de adicionar!! " + usuarioSelecionado);
        list.remove(usuarioSelecionado);
        setUsuariosCadastrados(list);
        listaUsuariosChamados.add(usuarioSelecionado);
        setUsuariosConvidado(listaUsuariosChamados);
        //  listaUsuariosChamados = new ArrayList<String>();
    }//GEN-LAST:event_adicionaConvidadoActionPerformed

    private void nomeCompromissoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeCompromissoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nomeCompromissoActionPerformed

    private void cadastraCompromissoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastraCompromissoActionPerformed

        if (!setUsuariosh().isEmpty()) {
            int confirmacao = JOptionPane.showConfirmDialog(null, "Deseja confirmar o cadastro do compromisso?");
            if (confirmacao == 0) {
                //OBS: Não estou pegando o nome do arquivo! Precisa resolver com vai enviar o arquivo! Tay.
                String emails = setUsuariosh();
                System.out.println("emails: " + emails);
                String DadosUsuario = ("CAD_COMP:" + DataCompromisso.getText() + ":" + horaInicio.getText() + ":" + horaTermino.getText() + ":" + cli.getEmailUsuarioConectado() + ":" + nomeCompromisso.getText() + ":" + localCompromisso.getText() + ":" + DescricaoCompromisso.getText() + ":" + "null" + ":" + "null" + ":" + listaUsuariosChamados.size() + ":" + emails /*arquivo.getName()*/);
                System.out.println(DadosUsuario);
                cli.TrataMsgsVindasServidor(DadosUsuario);
                //this.cli.usuarioConsultando();
                dispose();

            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Convide pelo menos uma pessoa para este compromisso!");
        }
        
        //cli.concatenaSinal(listUsersON);
    }//GEN-LAST:event_cadastraCompromissoActionPerformed

    private void AnexarArquivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnexarArquivoActionPerformed

        JFileChooser telaArquivo = new JFileChooser();
        telaArquivo.showOpenDialog(this);
        arquivo = telaArquivo.getSelectedFile();

        if (arquivo != null) {

            nomeArquivoAnexado.setText(telaArquivo.getName(arquivo));
            //compromisso.setAnexo(arquivo);

        }

    }//GEN-LAST:event_AnexarArquivoActionPerformed

    public void setUsuariosCadastrados(List<String> listaConvidados) {
        list = new ArrayList<String>(listaConvidados);
        ExibeListaUsuarios.setListData(list.toArray());
        // list = new ArrayList<String>();

    }

    public void setUsuariosConvidado(List<String> listaChamados) {
        ExibeListaConvidados.setListData(listaChamados.toArray());

    }

    public String setUsuarios() {
        String emails = new String();
        for (int i = 0; i < listaUsuariosChamados.size(); i++) {
            emails = emails + listaUsuariosChamados.get(i).concat(":");
            //  System.out.println("MMMMM:"+emails);
        }
        return emails;
    }

    public String setUsuariosh() {
        String emails = new String();
        for (int i = 0; i < listaUsuariosChamados.size(); i++) {
            emails = emails + listaUsuariosChamados.get(i).concat(":");
            //  System.out.println("MMMMM:"+emails);
        }
        return emails;
    }

    private void removeConvidadoSelecionadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeConvidadoSelecionadoActionPerformed
        if (!listaUsuariosChamados.isEmpty()) {
            String usuarioSelecionado = (String) ExibeListaConvidados.getSelectedValue();
            list.add(usuarioSelecionado);
            setUsuariosCadastrados(list);
            listaUsuariosChamados.remove(usuarioSelecionado);
            setUsuariosConvidado(listaUsuariosChamados);
        } else {
            JOptionPane.showMessageDialog(rootPane, "Não há convidados para serem removidos");
        }
        // TODO add your handling code here:
//        int indice = ExibeListaConvidados.getSelectedIndex();
//        if (indice > 0) {
//
//            listaConvidados.remove(indice);
//
//        } else {
//
//            JOptionPane.showMessageDialog(rootPane, "Não há convidados para serem removidos");
//
//        }
    }//GEN-LAST:event_removeConvidadoSelecionadoActionPerformed

    private void horaTerminoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_horaTerminoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_horaTerminoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaNovoCompromisso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaNovoCompromisso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaNovoCompromisso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaNovoCompromisso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaNovoCompromisso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AnexarArquivo;
    private javax.swing.JFormattedTextField DataCompromisso;
    private javax.swing.JTextArea DescricaoCompromisso;
    private javax.swing.JList ExibeListaConvidados;
    private javax.swing.JList ExibeListaUsuarios;
    private javax.swing.JLabel NomeAgendaEletronica;
    private javax.swing.JButton adicionaConvidado;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton cadastraCompromisso;
    private javax.swing.JFormattedTextField horaInicio;
    private javax.swing.JFormattedTextField horaTermino;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField localCompromisso;
    private javax.swing.JLabel nomeArquivoAnexado;
    private javax.swing.JTextField nomeCompromisso;
    private javax.swing.JButton removeConvidadoSelecionado;
    // End of variables declaration//GEN-END:variables
}
